import java.io.*;
import java.util.*;

/**
 * The Class Stutter.
 * @author Mouhamadou Lamine Seye
 */


public class Stutter {

	 /**
 	 * The main method.
 	 *
 	 * @param args the arguments
 	 * By default, input.txt is passed as a parameter
 	 */
	
 	public static void main(String[] args) {
		 
 		 
 		/**  
 		 * List to hold the data read from the file line by line
         */
 		 List<String> inputLines = new ArrayList<String>();
 		
		 try {
			 /**  
			  * Sets up a file reader to read the file passed on the command line one character at a time
	          */
			 FileReader input = new FileReader (args[0]); 
			 
			 /** 
			  * Filter FileReader through a Buffered read to read a line at a time
			 */
			 BufferedReader bufRead = new BufferedReader(input);
			 
			 /**  
			  * String to hold current line actually being read from the input file
			 */
			 String line;
			 
			 /**
			  * Read first line from input file and store it in the string line
			  */
			 line = bufRead.readLine();
			 
			 /**
			  * Adding the first line read from the input file to the list
			  */
			 inputLines.add(line);
			 
			 /**
			  * Read through input file one line at a time and store each line in the list inputLines
			  */
			 while (line != null){
				 line = bufRead.readLine();
				 if(line != null)
				 inputLines.add(line); 
			 }
			 
			//Close the input stream
			 bufRead.close();
		
		 }
		 
		 /**
		  * If no file was passed on the command line, this exception is generated. A message indicating how to the
		  * class should be called is displayed
		  */
		 catch (ArrayIndexOutOfBoundsException e){
			 System.out.println("Usage: java ReadFile filename\n");
		 }
		 
		 /**
		  * If another exception is generated, print a stack trace
		  */
		 catch (IOException e){
			 e.printStackTrace();
		 }
		 
 		 
 		/**  
 		 * List to hold the data generated by the method checkline.
 		 * The same data will be written to a file.
          */
 		 List<String> outputLines = new ArrayList<String>();
		 
 		 //call to checkline method
		 outputLines = checkLine(inputLines);
		 
		 //iterator to iterate through the output list 
		 Iterator it = outputLines.iterator();
		 
		 /*
		 while(it.hasNext() && it != null){
			 String theLine = (String)it.next();
			 System.out.println(theLine);
		 }*/
		 
		 //call to writeToFile method
		 writeToFile (outputLines);
		 
	}// end main 
 	
 	/**
	  * Check line.
	  * Check for stuttered words.
	  * @param inputLines the input lines
	  * @return the list outputLines
	  */
	 public static List<String> checkLine (List<String> inputLines) {
 	
		 //String list to hold the data to be returned
		 List<String> outputLines = new ArrayList<String>();
		 
		 //String array to hold each last word of a line
		 String[] lastWords = new String[inputLines.size()];
		  
		//iterator to iterate through the input list
		 Iterator it = inputLines.iterator();
		 
		 //index value to keep track of line numbers
		 int p = 1;
		  
		 //String to hold current line actually being read from the input list
		 String theLine;
		 
		 //loop will iterate through the input list looking for stuttered words
		 while(it.hasNext() && it != null){
			 
			 theLine = (String)it.next();
			 
			 //Call split method to break the line into an array of strings
			 String[] ary = theLine.split(" ");
			 
			 lastWords[p-1]= ary[ary.length-1];
			
			 //checking if previous line last word is equal to the current line first word
			 if(p >=2){
				 if(lastWords[p-2].equals(ary[0])){
				 String output = "Repeated word on line"+ " " + p + ": " + lastWords[p-2]+ " " + lastWords[p-2];
				 outputLines.add(output);
				 }
			 }
			 
			 //checking for stuttered words on the current line
			 for (int i=0;i<ary.length-1;i++){

				 if(ary[i].equals(ary[i+1])){
					 String output = "Repeated word on line"+ " " + p + ": " + ary[i] + " " + ary[i];
					 outputLines.add(output);
		 		 }
			 }

			 p++;
		 }
		 
		 return outputLines;
	 } // end checkLine method
	 
	 
	 /**
 	 * Write to file.
 	 * Write parameter list to the file output.txt
 	 * @param outputLines the output lines
 	 */
 	public static void writeToFile (List<String> outputLines) {
	 
		 try {
			 //Sets up a file writer to write to the file output.txt 
			 FileWriter writer = new FileWriter("output.txt");
			 BufferedWriter out = new BufferedWriter (writer);
			 PrintWriter print = new PrintWriter(out);
			 
			//iterator to iterate through the list outputLines
			 Iterator it = outputLines.iterator();

			 //writing each item on the list into the file
			 while(it.hasNext() && it != null){
				 String theLine = (String)it.next();
				 print.println(theLine);	
			 }
			//Close the output stream
			 print.close(); 
		 }
		 /**
		 * If an exception is generated, print a stack trace
		 */
		 catch (IOException e) {
			 // TODO Auto-generated catch block
			 e.printStackTrace();
		 }
	 } // end writeToFile method
} // end class

 		
